//
//  OMFancyListViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/18.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMFancyListViewController.h"
#import "OMConsumerGoodsCollectDelegater.h"
#import "OMGoodOneColumnLayout.h"
#import "OMNoLoginView.h"


@interface OMFancyListViewController ()
@property (nonatomic, strong) NSArray *goods;
@property (nonatomic, strong) UICollectionView *collectView;
/** collectView代理*/
@property (nonatomic, strong) OMConsumerGoodsCollectDelegater *delegater;
@property (nonatomic, strong) OMNoLoginView *loginView;
@end

@implementation OMFancyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.automaticallyAdjustsScrollViewInsets = false;
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[OMGoodOneColumnLayout new]];
    _collectView.backgroundColor = SM_whiteColor;
    _delegater = [[OMConsumerGoodsCollectDelegater alloc]initWithCollectionView:_collectView];
    _delegater.emptyDataMessage = @"还没买过东西呢";
    _delegater.emptyImage = [UIImage imageNamed:@"gouwuce"];
    [self.view addSubview:_collectView];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(self.navigationBar.mas_bottom);
    }];

    ///未登录遮罩
    self.loginView = [OMNoLoginView new];
    self.loginView.message = @"您还未登录，无法查看哟";
    [self.view addSubview:self.loginView];
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    _loginView.hidden = true;
    // Do any additional setup after loading the view.

    ///监听设置

    //1.登录监听
    @weakify(self);
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGIN_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_SHOW, nil);
        [self hideLoginView];
        [self getFancyList];
    }];
    //2.登出监听
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGOUT_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self showLoginView];
        SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_DISMISS, nil);
    }];

    ///数据请求(已登录时)
    if ([Storage isLogin]) {
        [self startAnimating:YES];
        [self getFancyList];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (![Storage isLogin]) {
        [self showLoginView];
    }
}


/**
 显示未登录页面
 */
- (void)showLoginView
{
    self.loginView.hidden = NO;
    [self.view bringSubviewToFront:self.loginView];
}


/**
 隐藏未登录页面
 */
- (void)hideLoginView
{
    self.loginView.hidden = YES;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //显示购物车
    if ([Storage isLogin]) {
        SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_SHOW, nil);
    }
}


/**
 数据重载
 */
- (void)reloadDataFromNetWork
{
    [self getFancyList];
}



/**
 获取我常买数据-network
 */
- (void)getFancyList
{
    [KTHttpUtils GET:@"member/buy.jhtml" completion:^(id responseObject, BOOL isSuccess, NSString *message) {
        if (isSuccess) {
            [self startAnimating:NO];
            NSArray <OMProduct*>*goods = [OMProduct mj_objectArrayWithKeyValuesArray:responseObject[OMDataKey]];
            for (OMProduct *product in goods) {
                product.image = [responseObject[OMImagePrefix] stringByAppendingString:product.image?:@""];
            }
            self.goods = goods;
        }else {
            [self showNetWorkError];
        }
    }];
}

- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    self.delegater.goods = goods;
}



@end
